﻿using System;

namespace StateMachine.TextParsing
{
    public abstract class TextParserContext<T> : StateMachineContext<T>
        where T : struct
    {
        private readonly T initialState;

        public TextParserContext(T initialState)
            : base(initialState)
        {
            this.initialState = initialState;
        }

        public int CurrentIndex { get; internal set; }

        internal void Initialize(string text)
        {
            this.Clear();
            this.InitializeOverride(text);
        }

        protected abstract void InitializeOverride(string text);

        internal void Clear()
        {
            this.State = this.initialState;
            this.CurrentIndex = 0;

            this.ClearOverride();
        }

        protected abstract void ClearOverride();
    }
}
